package com.gizwits.opensource.appkit.yasystem;

import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gizwits.opensource.appkit.R;

import java.io.InputStream;

public class Kind extends AppCompatActivity {
    Button button1,button2,button3,button4,button5;
    private LocalBroadcastManager localBroadcastManager;
    String data;
    int data1;
    int color = 0;
    boolean bRun = true;
    TextView textView;
    boolean bThread = false;
    private String smsg = "";    //显示用数据缓存
    private String fmsg = "";
    private String msg1 = "";
    private InputStream is;
    ImageView imageView;
    public BluetoothSocket _socket = null;      //蓝牙通信socket
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kind);

        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        button1 = (Button) findViewById(R.id.btn2);
        button2 = (Button) findViewById(R.id.btn3);
        imageView = (ImageView) findViewById(R.id.image);
        button3 = (Button) findViewById(R.id.btn4);
        button4 = (Button) findViewById(R.id.btn5);
        button5 = (Button) findViewById(R.id.btn6);


        imageView.setOnClickListener(onClickListener);
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
        button4.setOnClickListener(onClickListener);
        button5.setOnClickListener(onClickListener);

    }


    Button.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn2:
                    color = 1;
                    color(color);
                    data = "kind";
                    data1 =1;
                    retun(data,data1);
                    break;
                case R.id.btn3:
                    color = 2;
                    color(color);
                    data="kind";
                    data1 =2;
                    retun(data,data1);
                    break;
                case R.id.btn4:
                    color = 3;
                    color(color);
                    data="kind";
                    data1 =0;
                    retun(data,data1);
                    break;
                case R.id.image:
                    data = "SwindowE";
                    retun(data,data1);
                    Intent intent = new Intent();
                    // 专门用于向上一个活动返回数据。第一个参数用于向上一个活动返回结果码，一般只使用RESULT_OK或RESULT_CANCELED这两个值
                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                case R.id.btn5:
                    color = 4;
                    color(color);
                    data = "wan";
                    data1 = 2;
                    retun(data,data1);
                    break;
                case R.id.btn6:
                    color = 5;
                    color(color);
                    data ="wan";
                    data1 = 1;
                    retun(data,data1);
                    break;
                default:
                    break;
            }
        }
    };

    ///按钮颜色改变
    private  void color(int i){
        switch (i){
            case 1:
                button1.setBackgroundColor(Color.parseColor("#FF9500"));
                button3.setBackgroundColor(Color.parseColor("#F8DC26"));
                button2.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;
            case 2:
                button1.setBackgroundColor(Color.parseColor("#F8DC26"));
                button3.setBackgroundColor(Color.parseColor("#F8DC26"));
                button2.setBackgroundColor(Color.parseColor("#FF9500"));
                break;
            case 3:
                button1.setBackgroundColor(Color.parseColor("#F8DC26"));
                button3.setBackgroundColor(Color.parseColor("#FF9500"));
                button2.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;
            case 4:
                button5.setBackgroundColor(Color.parseColor("#F8DC26"));
                button4.setBackgroundColor(Color.parseColor("#FF9500"));
                break;
            case 5:
                button4.setBackgroundColor(Color.parseColor("#F8DC26"));
                button5.setBackgroundColor(Color.parseColor("#FF9500"));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        data = "SwindowE";
        retun(data,data1);
        Intent intent = new Intent();
        // 专门用于向上一个活动返回数据。第一个参数用于向上一个活动返回结果码，一般只使用RESULT_OK或RESULT_CANCELED这两个值
        setResult(RESULT_OK, intent);
        super.onDestroy();

    }

    public void retun(String data, int data1){
        Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
        intent.putExtra("isused",true);
        Log.d("TAG", String.valueOf(data)+"  "+String.valueOf(data1));
        intent.putExtra("kind",data1);
        intent.putExtra("key",data);
        intent.putExtra("data",true);
        localBroadcastManager.sendBroadcast(intent);
    }
}
